# Copyright (C) 2007 "dbsolution sas" 
#
# This file is part of Weebotee.
#
# Weebotee is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Weebotee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Weebotee.  If not, see <http://www.gnu.org/licenses/>.

from django.test import TestCase
from apps.core.models.section import Section
from apps.core.models.billing import Billing
from django.contrib.auth.models import User
from django.core import mail

class Frontend(TestCase):
    fixtures = ['resources/test.json']

    def test_registration(self):
        try:
            s = Section.objects.get(viewtype="core:Registration")
        except:
            s = None
        self.assert_(s, "No registration section found")

        response = self.client.get("/%s/"%s.slug)
        self.failUnlessEqual(response.status_code, 200, "Registration section returns %s instead of 200"%response.status_code)

        post = {"tiposervizio":"mio_sito", "username":"test", "password":"test", "password2":"test", "email":"test@test.com", "first_name":"test", "last_name":"test", "city":"Bari", "prov":"BA", "company":"test", "tel":"131415531", "fax":"13413513", "website":"http://www.test.com", "contract":True, "privacy":True, "utype":"client"}
        response = self.client.post("/admin/users/registration/", post)
        self.failUnlessEqual(response.status_code, 200, "Registration admin section returns %s instead of 200"%response.status_code)
        try:
            u = User.objects.get(username="test")
        except:
            u = None
        self.assert_(u, "Test user has not been registered")

    def test_forgot_password(self):
        try:
            s = Section.objects.get(viewtype="core:ForgotPassword")
        except:
            s = None
        self.assert_(s, "No forgot password section found")

        response = self.client.get("/%s/"%s.slug)
        self.failUnlessEqual(response.status_code, 200, "ForgotPassword section returns %s instead of 200"%response.status_code)

        u = User.objects.get(id=1)
        response = self.client.post("/%s/"%s.slug, {"email":u.email})
        self.failUnlessEqual(response.status_code, 200, "ForgotPassword POST returns %s instead of 200"%response.status_code)
        
        self.assertEqual(len(mail.outbox), 1, "ForgotPassword mail has not been send")

        import re

        password = re.search("\nnuova password: (?P<password>.*)\n\n", mail.outbox[0].body).group('password')
        r = self.client.login(username=u.username, password=password)

        self.assert_(r, "Login failed after changing password")

    def test_user_profile(self):
        u = User.objects.get(username="carlo")

        r = self.client.login(username=u.username, password="test")
        self.assert_(r, "Login failed")

        try:
            s = Section.objects.get(viewtype="core:Profile")
        except:
            s = None
        self.assert_(s, "No Profile section found")

        r = self.client.get("/%s/"%s.slug)
        self.failUnlessEqual(r.status_code, 200, "Profile section returns %s instead of 200"%r.status_code)

        post = {"email":"test@test.com", "password":"test", "first_name":"test", "last_name":"test", "web_site":"http://www.test.com", "address":"test", "city" :"test", "prov" : "tt", "cap":"12345", "company":"test", "tel":"012345", "fax":"012345", "piva":"12345678901"}

        r = self.client.post("/%s/"%s.slug, post)
        self.failUnlessEqual(r.status_code, 200, "Profile section returns %s instead of 200"%r.status_code)

        self.failUnlessEqual(u.udetail_set.all()[0].prov, "tt", "Profile isn't changed")

    def test_billing_data(self):
        u = User.objects.get(username="carlo")
        
        r = self.client.login(username=u.username, password="test")
        self.assert_(r, "Login failed")

        try:
            s = Section.objects.get(viewtype="core:Billing")
        except:
            s = None
        
        self.assert_(s, "No Billing section found")

        r = self.client.get("/billing/add/")
        self.failUnlessEqual(r.status_code, 200, "Billing add section returns %s instead of 200"%r.status_code)
        
        post = {"name":"test", "address":"test", "city":"test", "provincia":"BA", "cap":"12345", "codice_fiscale":"12345678901", "telefono":"1231451", "Legale_rappresentante":"test"}
        r = self.client.post("/billing/add/", post)
        
        try:
            b = Billing.objects.get(name="test")
        except:
            b = None

        self.assert_(b, "Billing object hasn't been created")


        r = self.client.get("/billing/%s/"%b.id)
        self.failUnlessEqual(r.status_code, 200, "Billing edit section returns %s instead of 200"%r.status_code)

        post = {"name":"test2", "address":"test", "city":"test", "provincia":"BA", "cap":"12345", "codice_fiscale":"12345678901", "telefono":"1231451", "Legale_rappresentante":"test"}
        r = self.client.post("/billing/%s/"%b.id, post)

        try:
            b = Billing.objects.get(id=b.id)
        except:
            b = None

        self.assertEqual(b.name,"test2", "Billing hasn't been modified")

        
